package pl.pw.mkmw.pnganimator.image.chunks;

import java.nio.ByteBuffer;
import pl.pw.mkmw.pnganimator.exception.ChunkNotInitializedException;
import pl.pw.mkmw.pnganimator.utils.ByteArrayUtils;

/**
 *
 * @author Michał
 */
public final class IHDRChunk extends ChunkImpl{
    
    private byte [] width;
    private byte [] height;
    private byte bitDepth = (byte)0;
    private byte colorType;
    private byte compressionMethod;
    private byte filterMethod;
    private byte interlaceMethod;

    public IHDRChunk() {
        super();
        this.chunkType = ChunkType.IHDR;
        this.compressionMethod = (byte) 0;
        this.filterMethod = (byte) 0;
        this.interlaceMethod = (byte) 0;
    }
    
    public IHDRChunk(int width, int height,BitDepth bd) {
        super();
        this.setDimensions(width, height);
        this.setBitDepth(bd);
        this.chunkType = ChunkType.IHDR;
        this.compressionMethod = (byte) 0;
        this.filterMethod = (byte) 0;
        this.interlaceMethod = (byte) 0;
    }
    
    public void setDimensions(int width, int height){
        this.width = ByteBuffer.allocate(4).putInt(width).array();
        this.height = ByteBuffer.allocate(4).putInt(height).array();
    }
    
    public void setBitDepth(BitDepth bd){
        switch(bd){
            case ONE:
                this.bitDepth = (byte) 1;
                break;
            case TWO:
                this.bitDepth = (byte) 2;
                break;
            case FOUR:
                this.bitDepth = (byte) 4;
                break;
            case EIGHT:
                this.bitDepth = (byte) 8;
                break;
            case SIXTEEN:
                this.bitDepth = (byte) 16;
                break;
        }
    }
    
    public void setColourType(ColourType ct){
        switch(ct){
            case GREYSCALE:
                this.colorType = (byte) 0;
                break;
            case TRUECOLOUR:
                this.colorType = (byte) 2;
                break;
            case INDEXED:
                this.colorType = (byte) 3;
                break;
            case GREYSCALE_ALPHA:
                this.colorType = (byte) 4;
                break;
            case TRUECOLOUR_ALPHA:
                this.colorType = (byte) 6;
                break;
        }
    }
    
    protected void generateChunkData() throws ChunkNotInitializedException{
        
        if(this.height == null || this.width == null || this.bitDepth == (byte)0){
            throw new ChunkNotInitializedException(this.getClass().getName()+": nie zainicjowanowszystkich parametrów");
        }
        byte [] tmp = {this.bitDepth,
            this.colorType, this.compressionMethod, this.filterMethod, this.interlaceMethod};
        byte [] cd = ByteArrayUtils.concate(ByteArrayUtils.concate(this.width, this.height), tmp);
        setChunkData(cd);
        
    }
    
    @Override
    public byte [] getChunk() throws ChunkNotInitializedException{
        generateChunkData();
        return super.getChunk();
    }
    
    
    public enum BitDepth{
        ONE,
        TWO,
        FOUR,
        EIGHT,
        SIXTEEN
    }
    
    public enum ColourType{
        GREYSCALE,
        TRUECOLOUR,
        INDEXED,
        GREYSCALE_ALPHA,
        TRUECOLOUR_ALPHA
    }
    
}
